package com.spa.infrastructure.repository.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.spa.infrastructure.repository.condition.DouYinCommentCondition;
import com.spa.infrastructure.repository.mapper.DouYinCommentMapper;
import com.spa.infrastructure.repository.po.DouYinCommentPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class DouYinCommentRepositoryService extends MPJBaseServiceImpl<DouYinCommentMapper, DouYinCommentPO> {

    @Resource
    private DouYinCommentMapper douYinCommentMapper;

    public Long addDouYinComment(DouYinCommentPO po) {
        douYinCommentMapper.insert(po);
        return po.getId();
    }

    public Page<DouYinCommentPO> page(DouYinCommentCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    private LambdaQueryWrapper<DouYinCommentPO> wrapper(DouYinCommentCondition condition) {
        LambdaQueryWrapper<DouYinCommentPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(condition.getItemId()),DouYinCommentPO::getReplyToItemId,condition.getItemId());
        wrapper.eq(StringUtils.isNotEmpty(condition.getParentId()),DouYinCommentPO::getParentId,condition.getParentId());
        return wrapper;
    }

}
